<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Team_entity{
	var $id = 0;
	var $sport_id = 0;
	var $name = "";
	var $flag = "";
	var $code = "";
	var $feed_team_id = "";
	
	function __destruct() {}
	function __construct() {}
    
	function init($cmd, $input)
	{
		$ci=& get_instance();
		if(!$input){
			return false;	
		}
		
		if($cmd == 'id')
		{
			$result = $ci->db->where('id',$input)->get(TBL_SPORT_TEAM)->result();
		}
		else if($cmd == 'feed_id'){
			$result = $ci->db->where('feed_team_id',$input)->get(TBL_SPORT_TEAM)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->name = $result[0]->name;
			$this->sport_id = $result[0]->sport_id;
			$this->flag = $result[0]->flag;
			$this->code = $result[0]->code;
			$this->feed_team_id = $result[0]->feed_team_id;
			
			// Create user folder if not exists
			$directory = "./uploads/default/teams";
			if(!file_exists($directory)){
				mkdir($directory);
				chmod( $directory, 0777 ) ;
				file_put_contents($directory."/index.php","");
				file_put_contents($directory."/index.html","");
			}
			$directory = "./uploads/default/teams/".$result[0]->id ;
			if (!file_exists ( $directory )) {
				mkdir($directory);
				chmod( $directory, 0777 ) ;
				file_put_contents($directory."/index.php","");
				file_put_contents($directory."/index.html","");
			}
			return $this;
		}
		return false;
	}
	
	function setName($input){ $this->name=$input; }
	function setSportId($input){ $this->sport_id=$input;}
	function setFlag($input){ $this->flag=$input; }
	function setCode($input){ $this->code=$input; }
	function setFeedTeamId($input){ $this->feed_team_id=$input; }
	
	function getId(){ return $this->id; }
	function getName(){ return $this->name; }
	function getFlag(){ return $this->flag;}
	function getSportId(){ return $this->sport_id;}
	function getCode(){ return $this->code; }
	function getFeedTeamId(){ return $this->feed_team_id; }
	
	function getFolderFileStorage(){ 
		return "./uploads/default/teams/".$this->id."/"; 
	}
	
	function getMyFlagTeam(){
		return $this->getFolderFileStorage().$this->getFlag();
	}
	
	function update()
	{
		$ci=& get_instance();
		 
		$ci->db->set('name', $this->name);
		$ci->db->set('flag', $this->flag);
		$ci->db->set('sport_id', $this->sport_id);
		$ci->db->set('code', $this->code);
		$ci->db->set('feed_team_id', $this->feed_team_id);
		
		$ci->db->where('id', $this->id)->update(TBL_SPORT_TEAM);
	}
	
	function createNewTeamEntity($name){
		$ci=& get_instance();
		
		$ci->db->set('name',$name);
		$ci->db->insert(TBL_SPORT_TEAM);
		
		return $ci->db->insert_id();
	}
	
	function getArrayListOfTeam(){
		$ci=& get_instance();
		return $ci->db->get(TBL_SPORT_TEAM)->result();
	}
	
	function getArrayIdAndNameOfTeam(){
		$array = array();
		foreach($this->getArrayListOfTeam() as $obj){
			$array[$obj->id] = $obj->code ? $obj->name . ' - '. $obj->code : $obj->name;
		}
		return $array;
	}
	
		
	//endclass
}
